<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="输入ID或ID范围用-隔开"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.product_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商家状态" prop="published">
        <el-select v-model="queryParams.published" placeholder="请选择商家状态" clearable>
          <el-option
            v-for="dict in dict.type.product_published"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="可售平台" prop="notAvailable">
        <el-select v-model="queryParams.notAvailable" placeholder="请选择可售平台" clearable>
          <el-option
            v-for="dict in dict.type.seller_platform"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="货源平台" prop="platform">
        <el-select v-model="queryParams.platform" placeholder="请选择货源平台" clearable>
          <el-option
            v-for="dict in dict.type.basic_platform"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="SPU" prop="spu">
        <el-input
          v-model="queryParams.spu"
          placeholder="请输入SPU"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SKU" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入SKU"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大类目" prop="className">
        <el-select v-model="queryParams.className" placeholder="请选择大类目" clearable>
          <el-option
            v-for="dict in dict.type.amazon_us_class_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小类目" prop="productType">
        <el-input
          v-model="queryParams.productType"
          placeholder="请输入小类目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="NodeID" prop="catalogId">
        <el-input
          v-model="queryParams.catalogId"
          placeholder="请输入NodeID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Node" prop="catalogName">
        <el-input
          v-model="queryParams.catalogName"
          placeholder="请输入Node"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="筛选大于等于此数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="筛选大于等于此价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商Code" prop="supplierCode">
        <el-input
          v-model="queryParams.supplierCode"
          placeholder="输入供应商Code"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可售地区" prop="region">
        <el-select v-model="queryParams.region" placeholder="请选择可售地区" clearable>
          <el-option
            v-for="dict in dict.type.product_region"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="修改者" prop="updateBy">
        <el-select v-model="queryParams.updateBy" placeholder="请选择修改者" clearable>
          <el-option
            v-for="dict in dict.type.update_by_name_list"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="图片数量" prop="imageCount">
        <el-input
          v-model="queryParams.imageCount"
          placeholder="图片数量大于等于此数值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序方式">
        <el-select v-model="sortForm.sort" placeholder="请选择排序方式" @change="handleSortChange">
          <el-option label="按ID升序" value="id,asc"></el-option>
          <el-option label="按ID降序" value="id,desc"></el-option>
          <el-option label="按修改时间升序" value="updateTime,asc"></el-option>
          <el-option label="按修改时间降序" value="updateTime,desc"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          plain-->
        <!--          icon="el-icon-plus"-->
        <!--          size="mini"-->
        <!--          @click="handleAdd"-->
        <!--          v-hasPermi="['basic:product:add']"-->
        <!--        >新增</el-button>-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:product:edit']"
        >修改
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['basic:product:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:product:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column width="80" label="ID" align="center" prop="id"/>
      <el-table-column width="80" label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column width="60" label="货源" align="center" prop="published">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_published" :value="scope.row.published"/>
        </template>
      </el-table-column>
      <el-table-column width="100" label="不可用" align="center" prop="notAvailable"/>
      <el-table-column label="平台" align="center" prop="platform">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basic_platform" :value="scope.row.platform"/>
        </template>
      </el-table-column>
      <el-table-column width="80" label="SPU" align="center" prop="spu" />
      <el-table-column width="80" label="SKU" align="center" prop="sku"/>
      <el-table-column label="大类" align="center" prop="className">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.amazon_us_class_name" :value="scope.row.className"/>
        </template>
      </el-table-column>
      <el-table-column width="80" label="小类" align="center" prop="productType"/>
      <el-table-column width="80" label="Node" align="center" prop="catalogName"/>
      <el-table-column width="60" label="Link" align="center">
        <template slot-scope="scope">
          <a :href="scope.row.link" target="_blank" style="color: #00afff;" rel="noopener noreferrer">点我</a>
        </template>
      </el-table-column>
      <el-table-column label="主图" align="center" prop="mainImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.mainImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip="true" width="300" label="标题" align="center" prop="title"/>
      <el-table-column width="120" label="商品尺寸" align="center" prop="dimensions"/>
      <el-table-column width="120" label="包装尺寸" align="center" prop="packages"/>
      <el-table-column width="70" label="价格" align="center" prop="price"/>
      <el-table-column  width="50" label="运费" align="center" prop="shipFee" />
      <el-table-column width="70" label="数量" align="center" prop="quantity"/>
      <el-table-column label="图片数量" align="center" prop="imageCount" />
      <el-table-column  show-overflow-tooltip="true" width="100" label="供应商CODE" align="center" prop="supplierCode" />
      <el-table-column  show-overflow-tooltip="true" width="100" label="供应商" align="center" prop="supplierName" />
      <el-table-column label="可售地区" align="center" prop="region">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_region" :value="scope.row.region"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip="true" width="120" label="备注" align="center" prop="remark"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改者" align="center" prop="updateBy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.update_by_name_list" :value="scope.row.updateBy"/>
        </template>
      </el-table-column>
      <el-table-column width="150" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:product:edit']"
          >修改
          </el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['basic:product:remove']"-->
<!--          >删除-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <BackToTop />
    <BackToBottom/>
    <!-- 添加或修改商品库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.product_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商家状态" prop="published">
          <el-select v-model="form.published" placeholder="请选择商家状态">
            <el-option
              v-for="dict in dict.type.product_published"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="不可用平台" prop="notAvailable">
          <el-input v-model="form.notAvailable"  placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="货源平台" prop="platform">
          <el-select v-model="form.platform" placeholder="请选择货源平台">
            <el-option
              v-for="dict in dict.type.basic_platform"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="SPU" prop="spu">
          <el-input v-model="form.spu" placeholder="请输入SPU" />
        </el-form-item>
        <el-form-item label="SKU" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入SKU"/>
        </el-form-item>
        <el-form-item label="大类" prop="className">
          <el-select v-model="form.className" placeholder="请选择大类目">
            <el-option
              v-for="dict in dict.type.amazon_us_class_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="小类" prop="productType">
          <el-input v-model="form.productType" placeholder="请输入小类目"/>
        </el-form-item>
        <el-form-item label="NodeID" prop="catalogId">
          <el-input v-model="form.catalogId" placeholder="请输入NodeID"/>
        </el-form-item>
        <el-form-item label="Node" prop="catalogName">
          <el-input v-model="form.catalogName" placeholder="请输入Node"/>
        </el-form-item>
        <el-form-item label="link" prop="link">
          <el-input v-model="form.link" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="主图" prop="mainImage">
          <image-preview width="100px" height="100px" :src="form.mainImage"/>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="商品尺寸" prop="dimensions">
          <el-input v-model="form.dimensions" placeholder="请输入商品尺寸"/>
        </el-form-item>
        <el-form-item label="包装尺寸" prop="packages">
          <el-input v-model="form.packages" placeholder="请输入包装尺寸"/>
        </el-form-item>
        <el-form-item label="反查关键词" prop="searchWords">
          <el-input v-model="form.searchWords" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格"/>
        </el-form-item>
        <el-form-item label="运费" prop="shipFee">
          <el-input v-model="form.shipFee" placeholder="请输入运费" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量"/>
        </el-form-item>
        <el-form-item label="供应商CODE" prop="supplierCode">
          <el-input v-model="form.supplierCode" placeholder="请输入供应商CODE" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplierName">
          <el-input v-model="form.supplierName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="可售地区" prop="region">
          <el-select v-model="form.region" placeholder="请选择可售地区">
            <el-option
              v-for="dict in dict.type.product_region"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="json_text" prop="jsonText">
          <el-input v-model="form.jsonText" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="产品信息" prop="productInfo">
          <el-input v-model="form.productInfo" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from '@/api/basic/product'
import globalMixins from '@/utils/globalMixins'

export default {
  name: 'Product',
  dicts: ['product_status', 'product_published', 'seller_platform', 'amazon_us_class_name', 'basic_platform','update_by_name_list','product_region'],
  data() {
    return {
      pickerOptions: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品库存表格数据
      productList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 产品信息时间范围
      daterangeCreateTime: [],
      // 查询参数
      sortForm: {
        sort: 'id,desc' // 默认值
      },
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        id: null,
        status: null,
        published: null,
        notAvailable: null,
        platform: null,
        spu: null,
        sku: null,
        className: null,
        productType: null,
        catalogId: null,
        catalogName: null,
        quantity: null,
        createTime: null,
        updateBy: null,
        supplierCode: null,
        supplierName: null,
        region: null,
        price: null,
        imageCount:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: '状态不能为空', trigger: 'change' }
        ],
        published: [
          { required: true, message: '商家状态不能为空', trigger: 'change' }
        ],
        notAvailable: [
          { required: false, message: '不可用平台不能为空', trigger: 'blur' }
        ],
        platform: [
          { required: true, message: '货源平台不能为空', trigger: 'blur' }
        ],
        sku: [
          { required: true, message: 'SKU不能为空', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    this.pickerOptions = globalMixins.data().pickerOptions
    this.$set(this.queryParams, 'orderByColumn', 'id');
    this.$set(this.queryParams, 'isAsc', 'desc');
    this.getList()
  },
  methods: {
    handleSortChange(value) {
      const [field, order] = value.split(',');
      // 更新查询参数
      this.$set(this.queryParams, 'orderByColumn', field);
      this.$set(this.queryParams, 'isAsc', order);
      // 根据新的查询参数重新获取列表数据
      this.getList();
    },
    /** 查询商品库存列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (this.queryParams.id && this.queryParams.id.includes('-')) {
        let parts = this.queryParams.id.split('-');
        this.queryParams.params['beginId'] = parts[0];
        this.queryParams.params['endId'] = parts[1];
      }
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        published: null,
        notAvailable: null,
        platform: null,
        spu: null,
        sku: null,
        className: null,
        productType: null,
        catalogId: null,
        catalogName: null,
        link: null,
        mainImage: null,
        title: null,
        dimensions: null,
        packages: null,
        searchWords: null,
        quantity: null,
        price: null,
        remark: null,
        jsonText: null,
        productInfo: null,
        createTime: null,
        updateTime: null,
        updateBy: null,
        supplierCode: null,
        supplierName: null,
        region: null,
        shipFee: null,
        imageCount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加商品库存'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改商品库存'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除商品库存编号为"' + ids + '"的数据项？').then(function() {
        return delProduct(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
